package com.xingxuecn.limit.dao.impl;

import com.xingxuecn.limit.common.model.dto.Role;
import com.xingxuecn.limit.common.model.vo.Page;
import com.xingxuecn.limit.common.utils.DbUtils;
import com.xingxuecn.limit.dao.RoleDao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang3.StringUtils;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Kris at 2018/9/11 10:24
 */
public class RoleDaoImpl implements RoleDao {
    QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());


    @Override
    public int add(Role role) throws SQLException {
        String sql = "insert into role(name,type,state) values(?,?,?)";
        Connection connection = DbUtils.getConnection();
        try {
            BigInteger id = queryRunner.insert(connection, sql, new ScalarHandler<BigInteger>(), new Object[]{
                    role.getName(),
                    role.getType(),
                    role.getState()
            });
            if (id != null) {
                role.setId(id.intValue());
            }
        } finally {
            connection.close();
        }
        return 1;
    }

    @Override
    public int update(Role role) throws SQLException {
        String sql = "update role set name=?,type=?,state=? where id=?";
        return queryRunner.update(sql, new Object[]{
                role.getName(),
                role.getType(),
                role.getState(),
                role.getId()
        });
    }

    @Override
    public int delete(int id) throws SQLException {
        String sql = "delete from role where id = ?";
        return queryRunner.execute(sql,id);
    }

    @Override
    public Page<Role> searchPage(Role role, int page, int rows) throws SQLException {
        return new Page<Role>(this.count(role),this.search(role,(page-1)*rows ,rows));
    }

    @Override
    public List<Role> search(Role role, Integer... limit) throws SQLException {
        StringBuffer sql = new StringBuffer("select * from role where 1=1");
        List<Object> jdbcParams = new LinkedList<>();
        if (role.getId() != null) {
            sql.append(" and id = ?");
            jdbcParams.add(role.getId());
        }
        if (StringUtils.isNotBlank(role.getName())) {
            sql.append(" and name like ?");
            jdbcParams.add("%" + role.getName() + "%");
        }
        if (role.getType() != null) {
            sql.append(" and type = ?");
            jdbcParams.add(role.getType());
        }
        if (limit != null && limit.length != 0) {
            if(limit.length == 1){
                sql.append(" limit ?");
                jdbcParams.add(limit[0]);
            }else if(limit.length == 2){
                sql.append(" limit ?,?");
                jdbcParams.add(limit[0]);
                jdbcParams.add(limit[1]);
            }else{
                throw new SQLException("limit最大长度为2");
            }
        }
        List<Role> result = queryRunner.query(sql.toString(), new BeanListHandler<Role>(Role.class), jdbcParams.toArray());
        return result;
    }

    @Override
    public int count(Role role) throws SQLException {
        StringBuffer sql = new StringBuffer("select count(*) from role where 1=1");
        List<Object> jdbcParams = new LinkedList<>();
        if (role.getId() != null) {
            sql.append(" and id = ?");
            jdbcParams.add(role.getId());
        }
        if (StringUtils.isNotBlank(role.getName())) {
            sql.append(" and name like ?");
            jdbcParams.add("%" + role.getName() + "%");
        }
        if (role.getType() != null) {
            sql.append(" and type = ?");
            jdbcParams.add(role.getType());
        }
        Long count = queryRunner.query(sql.toString(), new ScalarHandler<Long>(), jdbcParams.toArray());
        return count.intValue();
    }

    @Override
    public List<Role> findAll() throws SQLException {
        String sql = "select * from role";
        List<Role> roleList = queryRunner.query(sql,new BeanListHandler<Role>(Role.class));
        return roleList;
    }
}
